/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.MobileMessageRecorderDao;
import com.boxin.ims.modules.wechat.entity.MobileMessageRecorder;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 手机短信记录Service
 * @author zhangshanshan
 * @version 2015-08-08
 */
@Component
@Transactional(readOnly = true)
public class MobileMessageRecorderService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MobileMessageRecorderService.class);
	
	@Autowired
	private MobileMessageRecorderDao mobileMessageRecorderDao;
	
	public MobileMessageRecorder get(Long id) {
		return mobileMessageRecorderDao.findOne(id);
	}
	
	public Page<MobileMessageRecorder> find(Page<MobileMessageRecorder> page, MobileMessageRecorder mobileMessageRecorder) {
		DetachedCriteria dc = mobileMessageRecorderDao.createDetachedCriteria();
		if (mobileMessageRecorder.getUser()!=null && mobileMessageRecorder.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", mobileMessageRecorder.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(mobileMessageRecorder.getName())){
			dc.add(Restrictions.like("name", "%"+mobileMessageRecorder.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return mobileMessageRecorderDao.find(page, dc);
	}
	/**
	 * 根据手机号和验证码查询
	 * @param mobile
	 * @param code
	 * @return
	 */
	public MobileMessageRecorder findLostByMember(Long memberId) {
		
		DetachedCriteria dc = mobileMessageRecorderDao.createDetachedCriteria();
		dc.add(Restrictions.eq("memberId",memberId));
		List<MobileMessageRecorder> list = mobileMessageRecorderDao.find(dc);
		if(list.size()>0) {
			return list.get(list.size()-1);
		}else {
			return null;
		}
	}
	public MobileMessageRecorder findByMobileAndCode(String mobile,String code) {
		MobileMessageRecorder condtion = new MobileMessageRecorder();
		condtion.setMobile(mobile);
		condtion.setRandNum(code);
		
	
		return find(condtion);
	}
	public MobileMessageRecorder find(MobileMessageRecorder mobileMessageRecorder) {
		DetachedCriteria dc = mobileMessageRecorderDao.createDetachedCriteria();
		if (mobileMessageRecorder.getUser()!=null && mobileMessageRecorder.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", mobileMessageRecorder.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(mobileMessageRecorder.getName())){
			dc.add(Restrictions.like("name", "%"+mobileMessageRecorder.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(mobileMessageRecorder.getMobile())){
			dc.add(Restrictions.eq("mobile",mobileMessageRecorder.getMobile()));
		}
		if (StringUtils.isNotEmpty(mobileMessageRecorder.getRandNum())){
			dc.add(Restrictions.eq("randNum",mobileMessageRecorder.getRandNum()));
		}
		if(mobileMessageRecorder.getMemberId()!=null) {
			dc.add(Restrictions.eq("memberId",mobileMessageRecorder.getMemberId()));
		}
		dc.addOrder(Order.desc("id"));
		List<MobileMessageRecorder> list = mobileMessageRecorderDao.find(dc);
		return list!=null&&list.size()>0l?list.get(0):null;
	}
	
	@Transactional(readOnly = false)
	public void save(MobileMessageRecorder mobileMessageRecorder) {
		mobileMessageRecorderDao.save(mobileMessageRecorder);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		mobileMessageRecorderDao.deleteById(id);
	}
	

	
	/** 通过手机号，随机码查找有效数据 */
	public MobileMessageRecorder getByMobileAndRandNum(String mobile,String randNum){
		return mobileMessageRecorderDao.getByMobileAndRandNum(mobile, randNum);
	}
	
}
